<?php
require_once 'common.inc.php';

$filedir=dirname(__FILE__).'/';
$filename=$filedir.'filter.php';
//$filename='/www/push/filter.php';//字典文件
$tables=dirname(__FILE__).'/';//关键字文件
$syslevel=$filedir.'/syslevel.php';//系统过滤级别文件
if($action=='listlevel')
{
	$level=0;
	$count=4;
	$wordarr=array();
	if(file_exists($syslevel))
	{
		$fp=fopen($syslevel,'r+');
		$str='';
		$levels='';
		while($str=fread($fp,1024))
		{
			$levels.=$str;
		}
		fclose($fp);
		//s$levels=readfile($syslevel);//级别配置文件	
		
		$levelarr=explode(':',$levels);
		//print_r($levelarr);
		$level=$levelarr[0];
		$count=$levelarr[1];//级别总数
	}
	else
	{
		$fp=fopen($syslevel,'wb+');
		$content='1:4';
		fwrite($fp,$content);
		fclose($fp);
	}
	for($i=1;$i<=$count;$i++)
	{
		$wordfile=$tables.'table'.$i.'.txt';//显示某个级别的内容
		$fp=fopen($wordfile,'r+');
		$str='';
		$word='';
		while($str=fread($fp,1024))
		{
			$word.=$str;
		}
		$wordarr[$i]=explode('‖',$word);
		
	}
	switch($level)
	{
		case -1:$ifselect0='checked="checked"';break;
		case 1:$ifselect1='checked="checked"';break;
		case 2:$ifselect2='checked="checked"';break;
		case 3:$ifselect3='checked="checked"';break;
	}
	$ifselect4='checked=\"checked\"';
	$wordarr10=$wordarr[1][0];$wordarr11=$wordarr[1][1];$wordarr12=$wordarr[1][2];$wordarr13=$wordarr[1][3];$wordarr14=$wordarr[1][4];
	$wordarr20=$wordarr[2][0];$wordarr21=$wordarr[2][1];$wordarr22=$wordarr[2][2];$wordarr23=$wordarr[2][3];$wordarr24=$wordarr[2][4];
	$wordarr30=$wordarr[3][0];$wordarr31=$wordarr[3][1];$wordarr32=$wordarr[3][2];$wordarr33=$wordarr[3][3];$wordarr34=$wordarr[3][4];
	$wordarr40=$wordarr[4][0];$wordarr41=$wordarr[4][1];$wordarr42=$wordarr[4][2];$wordarr43=$wordarr[4][3];$wordarr44=$wordarr[4][4];
	include_once MCC_ROOT.'/templates/msystem/tfilter.htm';
}
//添加，编辑关键字。没有生产字典
elseif($action=='dokeytable')
{
	$type=intval(substr($keytable,5,1));
	
	$regxstr1 = str_replace("\\\\","\\",$regxstr1);
	$regxstr2 = str_replace("\\\\","\\",$regxstr2);
	$regxstr3 = str_replace("\\\\","\\",$regxstr3);
	$regxstr4 = str_replace("\\\\","\\",$regxstr4);
	switch($type)
	{
		case 1:$word=$commonk1.'‖'.$replacek1.'‖'.$forbidk1.'‖'.$wildcardstr1.'‖'.$regxstr1;break;
		case 2:$word=$commonk2.'‖'.$replacek2.'‖'.$forbidk2.'‖'.$wildcardstr2.'‖'.$regxstr2;break;
		case 3:$word=$commonk3.'‖'.$replacek3.'‖'.$forbidk3.'‖'.$wildcardstr3.'‖'.$regxstr3;break;
		case 4:$word=$commonk4.'‖'.$replacek4.'‖'.$forbidk4.'‖'.$wildcardstr4.'‖'.$regxstr4;break;
	}
	if($fp=fopen($tables.$keytable.'.txt','wb+'))
	{
		fwrite($fp,$word);
		fclose($fp);
	}	//include_once MCC_ROOT.'/templates/msystem/filter.htm';//跳转到提示管理员设置级别
	adminmsg('关键字设定成功，请设定系统过滤级别','filter.inc.php?action=listlevel');
}
//通过系统设置，生成字典
elseif($action=='dolevel')
{
	
	$filter=array();
	$dword=array();
	if($setlevel)//直接设定级别
	{
		//更新配置文件
		$content='';
		$fp=fopen($syslevel,'rb');
		while($str=fread($fp,1024))
		{
			$content.=$str;
		}
		$str=$setlevel.':'.substr($content,strpos($content,':')+1);
		fclose($fp);
		//取消级别设置
		if($fp=fopen($syslevel,'wb'))
		{
			fwrite($fp,$str);
			fclose($fp);
		}
		if($setlevel==-1)
		{
			$content='<?php ?>';
			writefile($filename,$content);
			adminmsg('取消系统过滤级别设置完成','filter.inc.php?action=listlevel');
		}
		
		//读取关键字文件		
		$fp=fopen($tables.'table'.$setlevel.'.txt','rb+');
		$str='';
		$word='';
		while($str=fread($fp,1024))
		{
			$word.=$str;
		}
		$wordarr=explode('‖',$word);
		$keyword=explode(';',$wordarr[0].';'.$wordarr[1].';'.$wordarr[2]);
		
		$comword=explode(';',$wordarr[0]);//普通		//不作处理，只是作个标记
		$repword=explode(';',$wordarr[1]);  //替换	//关键字要替换成指定符号
		$forbidden=explode(';',$wordarr[2]);//禁止	//含有此关键字的全部不显示
		if($wordarr[3]||$wordarr[4])
		{
			$regflag=1;//通配符和正则的标记符号
			$keyregx=explode(';',$wordarr[3]);  //通配符
			$keyregstr=explode(';',$wordarr[4]);//正则
		}
		else
		{
			$regflag=-1;
		}
	}
	else
	{
		adminmsg('设定过滤级别失败，请重新设定系统过滤级别','filter.inc.php?action=listlevle');
	}
	$typereg='';
	$typeword='';
	
	$filter=array();
	foreach($forbidden as $key=>$value)
	{
		$deny[$value]=1;
	}
	
	foreach($keyregx as $key=>$value)
	{
		$value=preg_replace('/{/','.{',$value);//通配符转换为正则
		if(strpos($value,'='))
		{
			$valuearr=explode('=',$value);
			$regx[$valuearr[0]]=$valuearr[1];//需要替换
		}	
		else
			$regx[$value]='-1';//没有替换		
	}	
	//正则的处理
	foreach($keyregstr as $key=>$value)
	{
		if(strpos($value,'='))
		{
			$valuearr=explode('=',$value);
			$regx[$valuearr[0]]=$valuearr[1];
		}	
		else
			$regx[$value]='-1';		
	}
/*
	level  1:不替换
	level  2:替换 即有等号
	level  3:禁止
*/
	foreach($keyword as $wordlist)
	{
	//	不替换但禁止
		if($deny[$wordlist])
		{
			$char=$wordlist;
			$repchar='';
			$level=3;
		}
		else
		{
//	替换
			if(strpos($wordlist,'='))
			{	
				$level=2;
				$list=explode('=',$wordlist);
				$char=$list[0];
				$repchar=$list[1];
			}
	//	不替换
			else
			{
				$level=1;
				$char=$wordlist;
				$repchar='';
			}
		}	
		$length=strlen($char);	
		for($n=0,$j=0;$n<$length;)
		{
			//$j=$i+1;
			$t = ord($char[$n]);
			if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) 
			{	
				 //ansic字符
				 $chars[$j]=substr($char,$n,1);
				 $n++; 
				 $j++;
			}
			elseif(194 <= $t && $t <= 223) 
			{
				$chars[$j]=substr($char,$n,2);
				 $n += 2; 
				 $j++;
			}
			elseif(224 <= $t && $t < 239) 
			{
				$chars[$j]=substr($char,$n,3);
				 $n += 3;
				 $j++;		//utf-8字符	}
			}	 
			elseif(240 <= $t && $t <= 247) 
			{
				$chars[$j]=substr($char,$n,4);
				 $n += 4;
				 $j++;
			} 
			elseif(248 <= $t && $t <= 251) 
			{
				$chars[$j]=substr($char,$n,5);
				 $n += 5;
				 $j++;
			}
			 elseif($t == 252 || $t == 253) 
			{
				$chars[$j]=substr($char,$n,6);
				 $n += 6; 
				 $j++;
			}
			 else 
			{
				$chars[$j]=substr($char,$n,1);
				$n++;
				$j++;
			}	
		}
		$length=$j;//字符长度,不是字节长度
		if($length<2)
			continue;
		for($i=0;$i<$length-2;$i++)
		{	
			$filter[$chars[$i]][$chars[$i+1]][0]=1;//没有考虑重复,标记包含关键字
		}
		
			if(1==$level)
			{	
				$filter[$chars[$i]][$chars[$i+1]][0]=1;
				$filter[$chars[$i]][$chars[$i+1]][99]=1;//结束标记
				$filter[$chars[$i]][$chars[$i+1]][$length]=2;//不替换
			}
			elseif(2==$level)
			{
				$filter[$chars[$i]][$chars[$i+1]][0]=1;
				$filter[$chars[$i]][$chars[$i+1]][99]=1;//结束标记
				$filter[$chars[$i]][$chars[$i+1]][$length]=3;//不替换
				$replace[$char]=$repchar;
			}
			elseif(3==$level)
			{
				$filter[$chars[$i]][$chars[$i+1]][0]=1;
				$filter[$chars[$i]][$chars[$i+1]][99]=1;//结束标记
				$filter[$chars[$i]][$chars[$i+1]][$length]=4;//不替换	
			}	
	}	
	//处理正则和通配符
	$content="<?php
				\$filter=array();
			  \$replace=array();
			  \$regx=array();
			  		";					
	foreach($filter as $key=>$arr)
	{
		foreach($arr as $index=>$values)
		{
			foreach($values as $type=>$rvalue)
		//$content.="\$filter['".$key."']='".$value."';";
			$content.="\$filter['".$key."']['".$index."'][".$type."]='".$rvalue."';".chr(0x0d).chr(0x0a);
		}
	}
	//替换部分
	foreach($replace as $key=>$value)
	{
		
			$content.="\$replace['".$key."']='".$value."';".chr(0x0d).chr(0x0a);
	}
	if($regflag==1)//正则的标记
	{
	//	通配符	
		foreach($regx as $key=>$value)
		{
			if($key!='')
				$content.="\$regx['".$key."']='".$value."';".chr(0x0d).chr(0x0a);
		}
	//	正则
	/*
		foreach($$wregxstr as $key=>$value)
		{
			$content.="\$regx['".$key."']='".$value."';";
		}
		*/
		$content.="\$regflag=1;";
	}
	else
		$content.="\$regflag=-1;";
	$content.=' ?>';
//	构造字符串，生成filter.php文件;
	writefile($filename,$content);
	adminmsg('系统过滤级别设定完成','filter.inc.php?action=listlevel');
}
//强制过滤字典列表生成
elseif($action=='doforce' )
{
	
	$filter=array();
	$dword=array();
	//if($setlevel)//直接设定级别
	//{
				
		//读取关键字文件		
		$fp=fopen($tables.'table4.txt','rb+');
		$str='';
		$word='';
		while($str=fread($fp,1024))
		{
			$word.=$str;
		}
		$wordarr=explode('‖',$word);
		$keyword=explode(';',$wordarr[0].';'.$wordarr[1].';'.$wordarr[2]);
		
		$comword=explode(';',$wordarr[0]);//普通		//不作处理，只是作个标记
		$repword=explode(';',$wordarr[1]);  //替换	//关键字要替换成指定符号
		$forbidden=explode(';',$wordarr[2]);//禁止	//含有此关键字的全部不显示
		if($wordarr[3]||$wordarr[4])
		{
			$regflag=1;//通配符和正则的标记符号
			$keyregx=explode(';',$wordarr[3]);  //通配符
			$keyregstr=explode(';',$wordarr[4]);//正则
		}
		else
		{
			$regflag=-1;
		}
	/* }
	else
	{
		adminmsg('设定过滤级别失败，请重新设定系统过滤级别','filter.inc.php?action=listlevle');
	} */
	$typereg='';
	$typeword='';
	
	$filter=array();
	foreach($forbidden as $key=>$value)
	{
		$deny[$value]=1;
	}
	
	foreach($keyregx as $key=>$value)
	{
		$value=preg_replace('/{/','.{',$value);//通配符转换为正则
		if(strpos($value,'='))
		{
			$valuearr=explode('=',$value);
			$regx[$valuearr[0]]=$valuearr[1];//需要替换
		}	
		else
			$regx[$value]='-1';//没有替换		
	}	
	//正则的处理
	foreach($keyregstr as $key=>$value)
	{
		if(strpos($value,'='))
		{
			$valuearr=explode('=',$value);
			$regx[$valuearr[0]]=$valuearr[1];
		}	
		else
			$regx[$value]='-1';		
	}
/*
	level  1:不替换
	level  2:替换 即有等号
	level  3:禁止
*/
	foreach($keyword as $wordlist)
	{
	//	不替换但禁止
		if($deny[$wordlist])
		{
			$char=$wordlist;
			$repchar='';
			$level=3;
		}
		else
		{
//	替换
			if(strpos($wordlist,'='))
			{	
				$level=2;
				$list=explode('=',$wordlist);
				$char=$list[0];
				$repchar=$list[1];
			}
	//	不替换
			else
			{
				$level=1;
				$char=$wordlist;
				$repchar='';
			}
		}	
		$length=strlen($char);	
		for($n=0,$j=0;$n<$length;)
		{
			//$j=$i+1;
			$t = ord($char[$n]);
			if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) 
			{	
				 //ansic字符
				 $chars[$j]=substr($char,$n,1);
				 $n++; 
				 $j++;
			}
			elseif(194 <= $t && $t <= 223) 
			{
				$chars[$j]=substr($char,$n,2);
				 $n += 2; 
				 $j++;
			}
			elseif(224 <= $t && $t < 239) 
			{
				$chars[$j]=substr($char,$n,3);
				 $n += 3;
				 $j++;		//utf-8字符	}
			}	 
			elseif(240 <= $t && $t <= 247) 
			{
				$chars[$j]=substr($char,$n,4);
				 $n += 4;
				 $j++;
			} 
			elseif(248 <= $t && $t <= 251) 
			{
				$chars[$j]=substr($char,$n,5);
				 $n += 5;
				 $j++;
			}
			 elseif($t == 252 || $t == 253) 
			{
				$chars[$j]=substr($char,$n,6);
				 $n += 6; 
				 $j++;
			}
			 else 
			{
				$chars[$j]=substr($char,$n,1);
				$n++;
				$j++;
			}	
		}
		$length=$j;//字符长度,不是字节长度
		if($length<2)
			continue;
		for($i=0;$i<$length-2;$i++)
		{	
			$filter[$chars[$i]][$chars[$i+1]][0]=1;//没有考虑重复,标记包含关键字
		}
		
			if(1==$level)
			{	
				$filter[$chars[$i]][$chars[$i+1]][0]=1;
				$filter[$chars[$i]][$chars[$i+1]][99]=1;//结束标记
				$filter[$chars[$i]][$chars[$i+1]][$length]=2;//不替换
			}
			elseif(2==$level)
			{
				$filter[$chars[$i]][$chars[$i+1]][0]=1;
				$filter[$chars[$i]][$chars[$i+1]][99]=1;//结束标记
				$filter[$chars[$i]][$chars[$i+1]][$length]=3;//不替换
				$replace[$char]=$repchar;
			}
			elseif(3==$level)
			{
				$filter[$chars[$i]][$chars[$i+1]][0]=1;
				$filter[$chars[$i]][$chars[$i+1]][99]=1;//结束标记
				$filter[$chars[$i]][$chars[$i+1]][$length]=4;//不替换	
			}	
	}	
	//处理正则和通配符
	$content="<?php
				\$filter1=array();
			  \$replace1=array();
			  \$regx1=array();
			  		";					
	foreach($filter as $key=>$arr)
	{
		foreach($arr as $index=>$values)
		{
			foreach($values as $type=>$rvalue)
		//$content.="\$filter['".$key."']='".$value."';";
			$content.="\$filter1['".$key."']['".$index."'][".$type."]='".$rvalue."';".chr(0x0d).chr(0x0a);
		}
	}
	//替换部分
	foreach($replace as $key=>$value)
	{
		
			$content.="\$replace1['".$key."']='".$value."';".chr(0x0d).chr(0x0a);
	}
	if($regflag==1)//正则的标记
	{
	//	通配符	
		foreach($regx as $key=>$value)
		{
			if($key!='')
				$content.="\$regx1['".$key."']='".$value."';".chr(0x0d).chr(0x0a);
		}
	//	正则
	/*
		foreach($$wregxstr as $key=>$value)
		{
			$content.="\$regx['".$key."']='".$value."';";
		}
		*/
		$content.="\$regflag1=1;";
	}
	else
		$content.="\$regflag1=-1;";
	$content.=' ?>';
//	构造字符串，生成filter.php文件;
	$filename="tfilter.php";
	writefile($filename,$content);
	adminmsg('系统强制过滤级别设定完成','filter.inc.php?action=listlevel');
}



function writefile($filename,$content)
{
	if($fp=fopen($filename,'wb+'))
	{
		fwrite($fp,$content);
		fclose($fp);
		return 1;
	}
	else
	{
		syslogs('can not create file');
		return false;
	}
}

?>